package com.zjbd.douyangcommonauth.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                //拦截请求
                .addPathPatterns("/**")
                //放行请求
                .excludePathPatterns(
                    "/doc.html",          // knife4j文档首页
                    "/doc.html/**",       // 文档页内部资源
                    "/v3/api-docs/**",    // OpenAPI接口文档数据
                    "/swagger-resources/**", // swagger资源文件
                    "/webjars/**"         // 文档依赖的前端资源
                )
                //以下是聚合用的接口，需要放行
                .excludePathPatterns(
                        "/douyang/live/v3/api-docs",
                        "/douyang/system/v3/api-docs",
                        "/douyang/auth/v3/api-docs"
                )
                //注册登录相关
                .excludePathPatterns(
                        "/douyang/auth/**"
                )
                .excludePathPatterns("/user/doLogin");
    }
}
